package com.tianyadu.security.core.validate.code.conf;

import com.tianyadu.security.core.properties.SecurityProperties;
import com.tianyadu.security.core.validate.code.sms.SmsCodeSender;
import com.tianyadu.security.core.validate.code.service.ValidateCodeGenerator;
import com.tianyadu.security.core.validate.code.sms.DefaultSmsCodeSender;
import com.tianyadu.security.core.validate.code.image.ImageCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @FileName: ValidateCodeBeanConfig
 * @Description:  验证码相关的扩展点配置。配置在这里的bean，业务系统都可以通过声明同类型或同名的bean来覆盖安全
 *                  模块默认的配置。
 * @author: xueyj
 * @create: 2018-12-11 16:04
 */
@Configuration
public class ValidateCodeBeanConfig {

    /** 获取Security配置属性 */
    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 图片验证码图片生成器
     * ConditionalOnMissingBean: 检查spring容器中是否存在imageValidateCodeGenerator相关bean信息，
     * 若不存在，则使用默认生成
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
    public ValidateCodeGenerator imageValidateCodeGenerator() {
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    /**
      * @Description: 发送短息验证码
      * @param:
      * @return:
      * @author:
      * @date:   2018/12/13 15:16
      */
    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender msCodeSender() {
        return new DefaultSmsCodeSender();
    }
}